﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static CCS_Snake.Contracts;

namespace CCS_Snake
{
    class Utils
    {
        /// <summary>
        /// 生成地图绘制参数
        /// </summary>
        /// <param name="rowCellCount"></param>
        /// <param name="colCellCount"></param>
        /// <param name="cellSize"></param>
        /// <param name="lineColor"></param>
        /// <param name="backgroundColor"></param>
        /// <returns></returns>
        public static MapPaintStruct MapPaint(int rowCellCount, int colCellCount, int cellSize, Color lineColor, Color backgroundColor)
        {
            MapPaintStruct mapPaint = new MapPaintStruct()
            {
                RowCellCount = rowCellCount,
                ColCellCount = colCellCount,
                CellSizeStep = cellSize + 1,
                LineColor = lineColor,
                BackgroundColor = backgroundColor,
            };

            return mapPaint;
        }

        public static SnakeColorStruct SnakeColor(Color head, Color body, Color tail, Color food)
        {
            SnakeColorStruct snakeColor = new SnakeColorStruct()
            {
                Head = head,
                Body = body,
                Food = food,
                Tail = tail,
            };

            return snakeColor;
        }

        /// <summary>
        /// 时间转换为数字
        /// </summary>
        /// <param name="timeStr"></param>
        /// <returns></returns>
        public static int TimeToInt(string timeStr)
        {
            string[] timeArray = timeStr.Split(':');
            int hour = int.Parse(timeArray[0]);
            int miniute = int.Parse(timeArray[1]);
            int second = int.Parse(timeArray[2]);

            return hour * 60 * 60 + miniute * 60 + second;
        }

        /// <summary>
        /// 数字转换为时间
        /// </summary>
        /// <param name="second"></param>
        /// <returns></returns>
        public static string IntToTime(int second)
        {
            int hour = (int)((float)second / 60 / 60);
            int miniute = (int)((((float)second / 60 / 60) - hour) * 60);
            int seconds = Convert.ToInt32((((float)second / 60) - (int)((float)second / 60)) * 60);

            return string.Format("{0:D2}:{1:D2}:{2:D2}", hour, miniute, seconds);
        }

        /// <summary>
        /// 随机生成贪吃蛇的移动方向
        /// </summary>
        /// <returns></returns>
        public static int RandomSnakeMoveDirection() { return new Random().Next(0, 4); }
        
        /// <summary>
        /// 随机生成食物坐标
        /// </summary>
        /// <param name="foodPoint"></param>
        /// <param name="mapPaint"></param>
        /// <param name="snakeBody"></param>
        /// <returns></returns>
        public static Point RandomFoodPoint(Point foodPoint, MapPaintStruct mapPaint, List<Point> snakeBody)
        {
            Point result = new Point(-1, -1);
            if (foodPoint == result)
            {
                // 生成未使用坐标列表
                List<Point> unusedPointList = new List<Point>();
                for (int rowIndex = 0; rowIndex < mapPaint.RowCellCount; rowIndex++)
                {
                    for (int colIndex = 0; colIndex < mapPaint.ColCellCount; colIndex++)
                    {
                        Point newPoint = new Point(rowIndex, colIndex);
                        if (!snakeBody.Contains(newPoint)) unusedPointList.Add(new Point(rowIndex, colIndex));
                    }
                }

                // 生成食物坐标
                int unusedPointCount = unusedPointList.Count;
                if (unusedPointCount > 0)
                {
                    int unusedPointIndex = new Random().Next(0, unusedPointCount);
                    result = unusedPointList[unusedPointIndex];
                }
            }

            return result;
        }

        /// <summary>
        /// 创建一条贪吃蛇
        /// </summary>
        /// <param name="mapPaint"></param>
        /// <param name="snakeDirection"></param>
        /// <returns></returns>
        public static List<Point> CreateSnakeBody(MapPaintStruct mapPaint, int snakeDirection)
        {
            // 根据贪吃蛇的移动方向, 默认创建一条 1 + 4 + 1 的贪吃蛇
            // 蛇头始终在地图中心
            int headX = Convert.ToInt32(mapPaint.ColCellCount / 2);
            int headY = Convert.ToInt32(mapPaint.RowCellCount / 2);

            Point snakeHead = new Point(headX, headY);

            // 根据蛇的运动方向创建蛇身
            List<Point> result = new List<Point> { snakeHead };
            for (int i = 1; i <= 5; i++)
            {
                int bodyX = -1;
                int bodyY = -1;
                switch (snakeDirection)
                {
                    case Parameter.MoveDirection.ToUp:
                        bodyX = snakeHead.X; bodyY = snakeHead.Y + i;
                        break;
                    case Parameter.MoveDirection.ToDown:
                        bodyX = snakeHead.X; bodyY = snakeHead.Y - i;
                        break;
                    case Parameter.MoveDirection.ToLeft:
                        bodyX = snakeHead.X + i; bodyY = snakeHead.Y;
                        break;
                    case Parameter.MoveDirection.ToRight:
                        bodyX = snakeHead.X - i; bodyY = snakeHead.Y;
                        break;
                }
                result.Add(new Point(bodyX, bodyY));
            }

            return result;
        }
    }
}
